package com.asymptote;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public abstract class MidObject implements Serializable {
	private static final long serialVersionUID = 2264778324046114824L;
	private DataEntity entity;

	protected MidObject() {
	}

	protected MidObject(DataEntity de) {
		entity = de;
	}

	public Date getEditTime() {
		return this.entity.getEditTime();
	}

	public String getEditTimeString() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return df.format( this.entity.getEditTime() );
	}

	public DataEntity getEntity() {
		return this.entity;
	}

	public long getId() {
		return getKey().getId();
	}

	public Key getKey() {
		return this.entity.getKey();
	}

	public String getKeyString() {
		return KeyFactory.keyToString(getKey());
	}

	public void setEditTime(Date editTime) {
		this.entity.setEditTime(editTime);
	}
	
	public void setEditTime() {
		this.entity.setEditTime();
	}

	protected void setEntity(DataEntity de) {
		this.entity = de;
	}

	public void remove() {
		PersistenceManager pm = DataUtil.getPersistenceManager();
		try {
			pm.deletePersistent(this.entity);
		} finally {
			pm.close();
		}
	}
}